<?php
/**
 * Text element
 *
 */
class ArrowFormError extends ArrowTag{
	
	
	public function configure(){
		$this->addProperties(array(
			"returnType"=>"html",
		));
		$this->setRequiredParent( "ArrowForm" );
	}
	
	public function generateOutput(){
		$form = $this->getParent("ArrowForm");
		
		$errors = $form->getErrors();
		
		$errorMsg = array();
		
		if($errors){
			foreach($errors as $name => $error){
				$errorMsg[$name] = $error;	
			}
		}
		$alerts = $form->getExternalAlerts();
		$toShow = array();
		if(!empty($errorMsg))
			$toShow = $errorMsg;
		if(!empty($alerts))
			$toShow = array_merge( $toShow, $alerts );
		
		if($this->getProperty("returnType") == "js")
			return json_encode(array( "errors" => $errorMsg, "alerts" => $alerts ));
			
		return $this->generateHtml($toShow);
	}
	
	private function generateHtml($toShow){
		$str = "<ul class=\"arrow-forms-error\">";
		foreach( $toShow as $alert ){
			if(is_array($alert)){
				foreach( $alert as $alert2 )
					$str.= "<li>".$alert2."</li>";
			}else
				$str.= "<li>".$alert."</li>";
		}
		$str.= "</ul>";
		return $str;
	}


}
?>